export default {
	// namespaced:true,
	mutations:{
		// 添加购物车数据
		addgooddetail(state,goods){
			// 判断数据仓库cart是否有商品信息
			// console.log(instanceof(state.cart));
			// 空数组直接不循环
			if(JSON.stringify(state.cart)=="[]"){
				state.cart.push(goods)
			}else{
				let result=state.cart.find((element)=>{
					if(element.goods_id==goods.goods_id){
						element.goods_count++;
					}
					return element.goods_id==goods.goods_id;
				})
				if(!result){
					state.cart.push(goods)
				}

			}
			 // 持久化存储vuex中数据
			 uni.setStorageSync('cart',JSON.stringify(state.cart))
			 
		},
		removeshop(state,id){
			state.cart=state.cart.filter((x)=>{
				return x.goods_id!=id
			})
		},
		// 购物车全选
		checkoutall(state){
			state.cart.forEach((e)=>{
				e.goods_state=true
			})
		}
	},
	getters:{
		shopcartotal(state){
			let c=0;
			state.cart.forEach((e)=>{
				
				
				 c+=e.goods_count;
				
			})
			 uni.setStorageSync("total",JSON.stringify(c))
			return c;
			// return state.shoptotal+3;
		},
		jiesuantotal(state,t){
			let num=0
			state.cart.filter((x)=>{
				return x.goods_state
			}).forEach((e)=>{
				num+=e.goods_count;
			})
			
			return num;
		},
		amount(state){
			let charge=0;
			state.cart.filter((x)=>{
				return x.goods_state
			}).forEach((e)=>{
				charge+=e.goods_price*e.goods_count;
			})
			return charge.toFixed(2);
		}
	},
	state:{
		cart:JSON.parse(uni.getStorageSync("cart"))|| []
		
	}
}